    package com.xuecheng.media.service.impl;

    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.j256.simplemagic.ContentInfo;
    import com.j256.simplemagic.ContentInfoUtil;
    import com.xuecheng.base.execption.XueChengPlusException;
    import com.xuecheng.base.model.po.PageParams;
    import com.xuecheng.base.model.po.PageResult;
    import com.xuecheng.base.model.po.RestResponse;
    import com.xuecheng.media.mapper.MediaFilesMapper;
    import com.xuecheng.media.mapper.MediaProcessMapper;
    import com.xuecheng.media.model.dto.QueryMediaParamsDto;
    import com.xuecheng.media.model.dto.UploadFileParamsDto;
    import com.xuecheng.media.model.dto.UploadFileResultDto;
    import com.xuecheng.media.model.po.MediaFiles;
    import com.xuecheng.media.model.po.MediaProcess;
    import com.xuecheng.media.service.MediaFileService;
    import io.minio.*;
    import io.minio.errors.*;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.codec.digest.DigestUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.jetbrains.annotations.Nullable;
    import org.springframework.beans.BeanUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.io.*;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDateTime;
    import java.util.Date;
    import java.util.List;

    @Slf4j
   @Service
   public class MediaFileServiceImpl extends ServiceImpl<MediaFilesMapper,MediaFiles> implements MediaFileService {

        @Autowired
        MediaFilesMapper mediaFilesMapper;

        @Autowired
        MediaProcessMapper mediaProcessMapper;

        //普通文件存储的桶
        @Value("${minio.bucket.files}")
        private String bucket_Files;
        //视频文件存储的桶
        @Value("${minio.bucket.videofiles}")
        private String bucket_videofiles;

        @Autowired
        private MinioClient minioClient;


        @Autowired
        MediaFileService currentProxy;


        @Override
        public PageResult<MediaFiles> queryMediaFiels(Long companyId, PageParams pageParams, QueryMediaParamsDto queryMediaParamsDto) {

            //构建查询条件对象
            LambdaQueryWrapper<MediaFiles> queryWrapper = new LambdaQueryWrapper<>();
            String filename = queryMediaParamsDto.getFilename();
            String fileType = queryMediaParamsDto.getFileType();
            String auditStatus = queryMediaParamsDto.getAuditStatus();
            queryWrapper.like(StringUtils.isNotBlank(filename), MediaFiles::getFilename, filename);
            queryWrapper.eq(StringUtils.isNotBlank(fileType), MediaFiles::getFileType, fileType);
            queryWrapper.eq(StringUtils.isNotBlank(auditStatus), MediaFiles::getAuditStatus, auditStatus);
            //分页对象
            Page<MediaFiles> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
            // 查询数据内容获得结果
            Page<MediaFiles> pageResult = mediaFilesMapper.selectPage(page, queryWrapper);
            // 获取数据列表
            List<MediaFiles> list = pageResult.getRecords();
            // 获取数据总数
            long total = pageResult.getTotal();
            // 构建结果集
            PageResult<MediaFiles> mediaListResult = new PageResult<>(list, total, pageParams.getPageNo(), pageParams.getPageSize());
            return mediaListResult;
        }

        @Override
        public UploadFileResultDto uploadFile(Long companyId, UploadFileParamsDto uploadFileParamsDto, byte[] bytes, String folder, String objectName) {

            //生成文件id，文件的md5值
            String fileId = DigestUtils.md5Hex(bytes);
            //文件名称
            String filename = uploadFileParamsDto.getFilename();
            //构造objectname
            if (StringUtils.isEmpty(objectName)) {
                objectName = fileId + filename.substring(filename.lastIndexOf("."));
            }
            if (StringUtils.isEmpty(folder)) {
                //通过日期构造文件存储路径
                folder = getFileFolder(new Date(), true, true, true);
            } else if (folder.indexOf("/") < 0) {
                folder = folder + "/";
            }
            //对象名称
            objectName = folder + objectName;
            MediaFiles mediaFiles = null;
            try {
                //上传至文件系统
                addMediaFilesToMinIO(bytes, bucket_Files, objectName);
                //写入文件表
                mediaFiles = currentProxy.addMediaFilesToDb(companyId, fileId, uploadFileParamsDto, bucket_Files, objectName);
                UploadFileResultDto uploadFileResultDto = new UploadFileResultDto();
                BeanUtils.copyProperties(mediaFiles, uploadFileResultDto);
                return uploadFileResultDto;
            } catch (Exception e) {
                e.printStackTrace();
                XueChengPlusException.cast("上传过程中出错");
            }
            return null;
        }

        public void addMediaFilesToMinIO(byte[] bytes, String bucket, String objectName) {
            //资源的媒体类型
            String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//默认未知二进制流

            if (objectName.indexOf(".") >= 0) {
                //取objectName中的扩展名
                String extension = objectName.substring(objectName.lastIndexOf("."));
                ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
                if (extensionMatch != null) {
                    contentType = extensionMatch.getMimeType();
                }
            }

            try {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

                PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                        .bucket(bucket)
                        .object(objectName)
                        //InputStream stream, long objectSize 对象大小, long partSize 分片大小(-1表示5M,最大不要超过5T，最多10000)
                        .stream(byteArrayInputStream, byteArrayInputStream.available(), -1)
                        .contentType(contentType)
                        .build();
                //上传到minio
                minioClient.putObject(putObjectArgs);
            } catch (Exception e) {
                e.printStackTrace();
                log.debug("上传文件到文件系统出错:{}", e.getMessage());
                XueChengPlusException.cast("上传文件到文件系统出错");
            }
        }

        @Transactional
        public MediaFiles addMediaFilesToDb(Long companyId, String fileId, UploadFileParamsDto uploadFileParamsDto, String bucket, String objectName) {
            //保存到数据库
            MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
            if (mediaFiles == null) {
                mediaFiles = new MediaFiles();

                //封装数据
                BeanUtils.copyProperties(uploadFileParamsDto, mediaFiles);
                mediaFiles.setId(fileId);
                mediaFiles.setFileId(fileId);
                mediaFiles.setCompanyId(companyId);
                mediaFiles.setBucket(bucket);
                mediaFiles.setFilePath(objectName);

                //获取扩展名
                String extension = null;
                String filename = uploadFileParamsDto.getFilename();
                if(StringUtils.isNotEmpty(filename) && filename.indexOf(".")>=0){
                    extension = filename.substring(filename.lastIndexOf("."));
                }
                //媒体类型
                String mimeType = getMimeTypeByextension(extension);
                //图片、mp4视频可以设置URL
                if(mimeType.indexOf("image")>=0 || mimeType.indexOf("mp4")>=0){
                    mediaFiles.setUrl("/" + bucket + "/" + objectName);
                }

                mediaFiles.setCreateDate(LocalDateTime.now());
                mediaFiles.setStatus("1");
                mediaFiles.setAuditStatus("002003");

                //插入文件表
                mediaFilesMapper.insert(mediaFiles);

                //对avi视频添加到待处理任务表
                if(mimeType.equals("video/x-msvideo")){
                    MediaProcess mediaProcess = new MediaProcess();
                    BeanUtils.copyProperties(mediaFiles,mediaProcess);
                    mediaProcessMapper.insert(mediaProcess);
                }
            }
            return mediaFiles;
        }


        //根据扩展名拿匹配的媒体类型
        private String getMimeTypeByextension(String extension){
            //资源的媒体类型
            String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//默认未知二进制流
            if(StringUtils.isNotEmpty(extension)){
                ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
                if (extensionMatch != null) {
                    contentType = extensionMatch.getMimeType();
                }
            }
            return  contentType;
        }
     @Override
     public RestResponse<Boolean> checkFile(String fileMd5) {
      MediaFiles mediaFiles = mediaFilesMapper.selectById(fileMd5);
      if (mediaFiles == null) {
       return RestResponse.success(false);
      }
      GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(mediaFiles.getBucket()).object(mediaFiles.getFilePath()).build();
      InputStream inputStream  = null;
      try {
       inputStream = minioClient.getObject(objectArgs);
       if (inputStream == null) {
        return RestResponse.success(false);
       }
      } catch (Exception e) {
       log.error("查询文件出现异常,文件ID为：{}",fileMd5);
       return RestResponse.success(false);
      }

      return RestResponse.success(true);
     }

     @Override
     public RestResponse<Boolean> checkChunk(String fileMd5, int chunkIndex) {
       String folderPath = getChunkFileFolderPath(fileMd5);
       String chunkFilePath=folderPath+chunkIndex;
      GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucket_videofiles).object(chunkFilePath).build();
      try {
       InputStream inputStream=minioClient.getObject(objectArgs);
       if (inputStream == null) {
        return RestResponse.success(false);
       }
      } catch (Exception e) {
       log.error("查找文件分块出现错误：文件Id：{},分块：{}", fileMd5, chunkIndex);
       return RestResponse.success(false);
      }
      return RestResponse.success(true);
     }

     //得到分块文件的目录
     private String getChunkFileFolderPath(String fileMd5) {
      return fileMd5.substring(0, 1) + "/" + fileMd5.substring(1, 2) + "/" + fileMd5 + "/" + "chunk" + "/";
     }

     @Override
     public RestResponse uploadChunk(String fileMd5, int chunk, byte[] bytes) {
      String folderPath = getChunkFileFolderPath(fileMd5);
      String chuckFileName = folderPath + chunk;
      try {
       addMediaFilesToMinIO(bytes, bucket_videofiles, chuckFileName);
       return RestResponse.success(true);
      } catch (Exception exception) {
       log.error("上传分块文件：{}，失败：{}",chuckFileName,exception.getMessage());

      }
      return RestResponse.validfail(false, "上传分块文件失败");

     }
        //将文件上传到文件系统
        public void addMediaFilesToMinIO(String filePath, String bucket, String objectName){
            try {
                UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                        .bucket(bucket)
                        .object(objectName)
                        .filename(filePath)
                        .build();
                //上传
                minioClient.uploadObject(uploadObjectArgs);
                log.debug("文件上传成功:{}",filePath);
            } catch (Exception e) {
                XueChengPlusException.cast("文件上传到文件系统失败");
            }
        }

        @Override
        public RestResponse<String> deleteMedia(String mediaId) {
            return null;
        }

        //合并分块
        @Override
        public RestResponse mergeChunks(Long companyId, String fileMd5, int chunkTotal, UploadFileParamsDto uploadFileParamsDto) {
            //下载分块
            File[] chunkFiles = checkChunkStatus(fileMd5, chunkTotal);

            //得到合并后文件的扩展名
            String filename = uploadFileParamsDto.getFilename();
            //扩展名
            String extension = filename.substring(filename.lastIndexOf("."));
            File tempMergeFile = null;
            try {
                try {
                    //创建一个临时文件作为合并文件
                    tempMergeFile = File.createTempFile("merge", extension);
                } catch (IOException e) {
                    XueChengPlusException.cast("创建临时合并文件出错");
                }
                //创建合并文件的流对象
                try( RandomAccessFile raf_write  =new RandomAccessFile(tempMergeFile, "rw")) {
                    byte[] b = new byte[1024];
                    for (File file : chunkFiles) {
                        //读取分块文件的流对象
                        try(RandomAccessFile raf_read = new RandomAccessFile(file, "r");) {
                            int len = -1;
                            while ((len = raf_read.read(b)) != -1) {
                                //向合并文件写数据
                                raf_write.write(b, 0, len);
                            }
                        }
                    }
                } catch (IOException e) {
                    XueChengPlusException.cast("合并文件过程出错");
                }
                //校验合并后的文件是否正确
                try {
                    FileInputStream mergeFileStream = new FileInputStream(tempMergeFile);
                    String mergeMd5Hex = DigestUtils.md5Hex(mergeFileStream);
                    if (!fileMd5.equals(mergeMd5Hex)) {
                        log.debug("合并文件校验不通过,文件路径:{},原始文件md5:{}", tempMergeFile.getAbsolutePath(), fileMd5);
                        XueChengPlusException.cast("合并文件校验不通过");
                    }
                } catch (IOException e) {
                    log.debug("合并文件校验出错,文件路径:{},原始文件md5:{}", tempMergeFile.getAbsolutePath(), fileMd5);
                    XueChengPlusException.cast("合并文件校验出错");
                }

                //拿到合并文件在minio的存储路径
                String mergeFilePath = getFilePathByMd5(fileMd5, extension);
                //将合并后的文件上传到文件系统
                addMediaFilesToMinIO(tempMergeFile.getAbsolutePath(), bucket_videofiles, mergeFilePath);
                //将文件信息入库保存
                uploadFileParamsDto.setFileSize(tempMergeFile.length());//合并文件的大小
                addMediaFilesToDb(companyId, fileMd5, uploadFileParamsDto, bucket_videofiles, mergeFilePath);

                return RestResponse.success(true);
            }finally {
                //删除临时分块文件
                if(chunkFiles!=null){
                    for (File chunkFile : chunkFiles) {
                        if(chunkFile.exists()){
                            chunkFile.delete();
                        }
                    }
                }
                //删除合并的临时文件
                if(tempMergeFile!=null){
                    tempMergeFile.delete();
                }
            }
        }

        @Override
        public MediaFiles getFileById(String id) {
            MediaFiles mediaFiles = mediaFilesMapper.selectById(id);
            if(mediaFiles==null){
                XueChengPlusException.cast("文件不存在");
            }
            String url = mediaFiles.getUrl();
            if(StringUtils.isEmpty(url)){
                XueChengPlusException.cast("文件还没有处理，请稍后预览");
            }

            return mediaFiles;
        }

        private String getFilePathByMd5(String fileMd5,String fileExt){
            return   fileMd5.substring(0,1) + "/" + fileMd5.substring(1,2) + "/" + fileMd5 + "/" +fileMd5 +fileExt;
        }

        /**
         * @description 下载分块
         * @param fileMd5
         * @param chunkTotal 分块数量
         * @return java.io.File[] 分块文件数组
         */
        private File[] checkChunkStatus(String fileMd5, int chunkTotal ){

            //得到分块文件所在目录
            String chunkFileFolderPath = getChunkFileFolderPath(fileMd5);
            //分块文件数组
            File[] chunkFiles = new File[chunkTotal];
            //开始下载
            for (int i = 0; i < chunkTotal; i++) {
                //分块文件的路径
                String chunkFilePath = chunkFileFolderPath + i;
                //分块文件
                File chunkFile = null;
                try {
                    chunkFile = File.createTempFile("chunk", null);
                } catch (IOException e) {
                    e.printStackTrace();
                    XueChengPlusException.cast("创建分块临时文件出错"+e.getMessage());
                }
                //下载分块文件
                downloadFileFromMinIO(chunkFile, bucket_videofiles, chunkFilePath);
                chunkFiles[i] = chunkFile;
            }
            return chunkFiles;
        }

        //根据桶和文件路径从minio下载文件
        public File downloadFileFromMinIO(File file,String bucket,String objectName){

            GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucket).object(objectName).build();
            try(
                    InputStream inputStream = minioClient.getObject(getObjectArgs);
                    FileOutputStream outputStream =new FileOutputStream(file);
            ) {
                IOUtils.copy(inputStream,outputStream);
                return file;
            }catch (Exception e){
                e.printStackTrace();
                XueChengPlusException.cast("查询分块文件出错");
            }
            return null;
        }

        //根据日期拼接目录
     private String getFileFolder(Date date, boolean year, boolean month, boolean day) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      //获取当前日期字符串
      String dateString = sdf.format(new Date());
      //取出年、月、日
      String[] dateStringArray = dateString.split("-");
      StringBuffer folderString = new StringBuffer();
      if (year) {
       folderString.append(dateStringArray[0]);
       folderString.append("/");
      }
      if (month) {
       folderString.append(dateStringArray[1]);
       folderString.append("/");
      }
      if (day) {
       folderString.append(dateStringArray[2]);
       folderString.append("/");
      }
      return folderString.toString();
     }

    }
